/*
    i.print.js      version 1.0.0      
    Copyright (C) 2013  sympol solutions
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://sympol.net/legal/gpl/.
*/
(function (context) {
    //some easily changable settings
    var extend=function(destination,source){
       for(var prop in source){
           destination[prop]=source[prop];
       }
       return destination;
    };
    
    var iPrintConfig=window.iPrintOptions||{};
    var iPrintSettings={
        winProp : {
            height: "500",  
            width: "500",        
            resize: 'yes',             
            scrollbars: 'yes',         
            menubar: 'no',             
            status: 'no',              
            directories: 'no',        
            toolbar: 'no',             
            location: 'no',             
            copyhistory: 'no'          
        },
        btnProp : {
            fontSize: 14,               
            color: "#a08430",
            background: "#e7f0fc",
            borderSize: 1,
            borderStyle: "solid",
            borderColor: "#cfe1f9",
            text: 'Print'
        },
        closeOnAction : false      //close print window when an action has been taken in the print properties dialog
    };
    var _=extend(iPrintSettings,iPrintConfig);
    //check if window is in a frame, if so do our thing
    if (window.self !== window.top) {
        //create static link
        var a = document.createElement('span'),
        //set printable window attributes
    features = "toolbar=" + _.winProp.toolbar + ",location=" + _.winProp.location + ",directories=" + _.winProp.directories + 
                ",status=" + _. winProp.status + ",menubar=" + _.winProp.menubar + ",scrollbars=" + _.winProp.scrollbars + 
                ",resizable=" + _.winProp.resize + ",copyhistory=" + _.winProp.copyhistory + ",width=" + _.winProp.width + ",height=" + _.winProp.height,
        //object containing the style for the print link
        style = [{ display: "inline" }, { position: "fixed" }, { top: 0 }, { right: 0 },
    { border: _.btnProp.borderSize + 'px ' + _.btnProp.borderStyle + ' ' + _.btnProp.borderColor }, { background: _.btnProp.background },
    { padding: "0" }, { margin: "0" }, { fontSize: _.btnProp.fontSize + 'pt' }, { color: _.btnProp.color }, { cursor: "pointer" }],
        //miscellaneous varaiables
    i = 0;
        //loop through styles and apply to element
        for (; i < style.length; i += 1) {
            var p = style[i];
            for (var prop in p) {
                a.style[prop] = p[prop];
            }
        }
        //set the id so we can reference back to the link for removal
        a.id = "sympolPrintWrap";
        //create and append the print link to the wrapper span
        a.innerHTML = "<a id='sympolIframePrinter' style='display:block; width:100%;'>" + _.btnProp.text + "</a>";
        // add the wrapper to the top of the body
        var $oldPadding=context.style.paddingTop?context.style.paddingTop:0;
        context.style.paddingTop=_.btnProp.fontSize+10+"px";
        context.insertBefore(a, context.firstChild);
        //create the onclick event
        document.getElementById('sympolIframePrinter').onclick = function () {
            //b=window.location     **8/6/13 elb **
            //get a copy of the iframes markup
            var b = document.getElementsByTagName('html')[0].innerHTML;
            //get the full markup of the print button
            var elem = document.getElementById('sympolPrintWrap').outerHTML;
            //var c=window.open(b,"iframePrintable", features, false);           **8/6/13 elb **
            var c = window.open(window.location, "iframePrintable", features, false);
            //write to the new window            **8/6/13 elb **
            c.document.write(b.replace(elem, ''));
            //close the document to stop editing and allow printing
            c.document.body.style.paddingTop=isNaN(parseInt($oldPadding,10))? $oldPadding + "px":$oldPadding;
            c.document.close();
            //focus on it
            c.focus();
            //continue the print
            c.print();
            if (_.closeOnAction) {
                //close the window after printing, wait a few on IE as it will close before the print dialog has displayed
                if (navigator.appName !== 'Microsoft Internet Explorer') {
                    c.close();
                } else {
                    c.setTimeout(function () { c.close(); }, 1500);
                }
            }
        };
    }
    //set document.body as our context
})(document.body);